﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows;
using System.ComponentModel;

namespace Kokomo.PeInspector.CliMetadata
{
	class DataGridRowIndexConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			DataGridRow row = value as DataGridRow;
			if (row != null)
			{
				ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(row);
				object item = itemsControl.ItemContainerGenerator.ItemFromContainer(row);

				IBindingList bindingList = itemsControl.ItemsSource as IBindingList;
				if (bindingList != null)
				{
					int rowIndex = bindingList.IndexOf(item);
					if (rowIndex < 0) return DependencyProperty.UnsetValue;
					return rowIndex + 1;
				}
			}

			return DependencyProperty.UnsetValue;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
